package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 添加购物车
 *
 * @Author Yang
 * @Date 2023/3/9 9:58
 */
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车参数
    @PostMapping("/cart/add")
    public ResultInfo add(@RequestBody Cart cartParam){
        Cart cart = cartService.add(cartParam);//将购物车返回前端
        return ResultInfo.success(cart);
    }


    // 查看购物车列表
    @GetMapping("/cart/list")
    public ResultInfo list(){
        // 1.调用service查询
        List<Cart> list = cartService.list();
        // 2.返回结果
        return ResultInfo.success(list);
    }

    //清空购物车
    @DeleteMapping("cart/clean")
    public ResultInfo delete(){
        cartService.delete();
        return ResultInfo.success();
    }

    //减少购物车参数
    @PostMapping("/cart/sub")
    public ResultInfo sub(@RequestBody Cart cartParam){
        Cart cart = cartService.sub(cartParam);
        return ResultInfo.success(cart);
    }
}
